﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using Utils;

namespace UI.ModSalary
{
    public partial class EmpItemChangeAdd : System.Web.UI.Page
    {
        EmployeeBiz employeeBiz = new EmployeeBiz();
        EmpItemChangeBiz empChangeBiz = new EmpItemChangeBiz();
        DefDicBiz defDicBiz = new DefDicBiz();
        SetsItemBiz setsItemBiz = new SetsItemBiz();
        EmpItemBiz empItemBiz = new EmpItemBiz();
        string changeID = "0";//By Update
        string account = "";
        protected void Page_Init(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (Request.Params["ChangeID"] != null)
            {
                changeID = Request.Params["ChangeID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            if (!IsPostBack)
            {
                defDicBiz.InitDropDownList("Wage_Sets", ddlSetsID, "1");
                DataTable dt = setsItemBiz.GetItemDict("1", "part");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlItem.Items.Add(new ListItem(dt.Rows[i]["ItemName"].ToString(), dt.Rows[i]["WageItem"].ToString()));
                }
                defDicBiz.InitDropDownList("Admin_Change_type", ddlChangeTypeName, "晋升");
                //ddlChangeTypeName
                if (changeID != "0")
                    BindData(changeID);
                else
                { 
                    defDicBiz.InitSynDropDownList(ddlItem,"职补");              
                    txtChangeTime.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
                }
                JSUtility.AddConfirm(btnDelete, "您真的要删除该职位变动的信息吗？");


            }
        }
        protected void ddlSetsID_SelectedIndexChanged(object sender, EventArgs e)
        {
            string setsID = ddlSetsID.SelectedValue;
            if (setsID == "0") return;
           
            DataTable dt = setsItemBiz.GetItemDict(setsID, "part");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ddlItem.Items.Add(new ListItem(dt.Rows[i]["ItemName"].ToString(), dt.Rows[i]["WageItem"].ToString()));
            }
        }
        private void BindData(string changeID)
        {
            DataTable dt = empChangeBiz.GetOneInfo(changeID);
            if (dt.Rows.Count > 0)
            {
                txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                txtZipCode.Text = dt.Rows[0]["ZipCode"].ToString();
                txtOldAllowance.Text = dt.Rows[0]["OldAllowance"].ToString();
                txtNewAllowance.Text = dt.Rows[0]["NewAllowance"].ToString();
                defDicBiz.InitDropDownList(ddlSetsID, dt.Rows[0]["SetsID"].ToString());
                defDicBiz.InitSynDropDownList(ddlChangeTypeName, dt.Rows[0]["ChangeTypeName"].ToString());
                defDicBiz.InitSynDropDownList2(ddlItem, dt.Rows[0]["WageItem"].ToString()); 
              
                string ChangeTime = dt.Rows[0]["ChangeTime"].ToString();
                if (Utils.RegexRule.IsDate(ChangeTime))
                    txtChangeTime.Text = Convert.ToDateTime(ChangeTime).ToString("yyyy-MM-dd");

                txtMemo.Text = dt.Rows[0]["Memo"].ToString();

            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }
        protected void txtEmpCode_TextChanged(object sender, EventArgs e)
        {
            string empCode = txtEmpCode.Text.Trim();
            if (!string.IsNullOrWhiteSpace(empCode))
            {
                if (Utils.StringUtility.IncludeHZ(empCode))//如果包含汉字
                {
                    DataTable dt = employeeBiz.GetOneInfoByName(empCode);
                    if (dt.Rows.Count == 0)
                    {
                        JSUtility.Alert(this, "不存在该员工姓名: " + empCode);
                    }
                    else if (dt.Rows.Count == 1)
                    {
                        if (dt.Rows[0]["StateName"].ToString() != "离职")
                        {
                            txtEmpCode.Text = dt.Rows[0]["EmpCode"].ToString();
                            txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                            txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                            txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                            txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                            txtZipCode.Text = dt.Rows[0]["ZipCode"].ToString();

                            txtOldAllowance.Text = new EmpItemBiz().GetWageItemValue(dt.Rows[0]["EmpCode"].ToString(), ddlItem.SelectedValue); 
                           //找出变动前的职补
                           

                        }
                        else
                            JSUtility.Alert(this, "该员工已经离职: " + empCode);
                    }
                    else
                    {
                        string info = "存在同名，请输入工号：\n\n";
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            info += dt.Rows[i]["EmpCode"].ToString() + "  " + dt.Rows[i]["EmpName"].ToString() + "\n";
                        }
                        JSUtility.Alert(this, info);
                    }
                }
                else
                {
                    DataTable dt = employeeBiz.GetOneInfo(empCode);
                    if (dt.Rows.Count > 0)
                    {
                        txtDeptName.Text = dt.Rows[0]["DeptName"].ToString();
                        txtDutyName.Text = dt.Rows[0]["DutyName"].ToString();
                        txtEmpName.Text = dt.Rows[0]["EmpName"].ToString();
                        txtStateName.Text = dt.Rows[0]["StateName"].ToString();
                        txtZipCode.Text = dt.Rows[0]["ZipCode"].ToString();

                        txtOldAllowance.Text = new EmpItemBiz().GetWageItemValue(empCode, ddlItem.SelectedValue); 
                    }
                    else
                        JSUtility.Alert(this, "不存在该员工编号: " + empCode);
                }
            }
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpItemChangeAdd.aspx", RightChar.Add))
            {
                if (!AccountBiz.CheckRight(account, "EmpItemChangeList.aspx", RightChar.Add))
                {
                    JSUtility.Alert(this, "您没有增加权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string wageItem = ddlItem.SelectedValue;
            if (wageItem == "0" || ddlSetsID.SelectedValue == "0" || ddlChangeTypeName.SelectedValue == "0")
            {
                JSUtility.Alert(this, "请选择帐套、工资项、变动内容");
                return;
            }
            string changeTime = txtChangeTime.Text.Trim();
            if (!RegexRule.IsDate(changeTime))
            {
                JSUtility.Alert(this, "离职时间无效");
                return;
            }
          string oldAllowance = txtOldAllowance.Text.Trim();
          string newAllowance = txtNewAllowance.Text.Trim();

          if (!RegexRule.IsDecimal(oldAllowance) || !RegexRule.IsDecimal(newAllowance))
            {
                JSUtility.Alert(this, "数字格式必须正确");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();

            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("StateName", txtStateName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());          
            
            sdList.Add("ZipCode", txtZipCode.Text.Trim());           
            sdList.Add("ChangeTypeName", ddlChangeTypeName.SelectedItem.Text.Trim());
            sdList.Add("ChangeTime", changeTime);
            sdList.Add("SetsID", ddlSetsID.SelectedValue);
            sdList.Add("WageItem", wageItem);
            sdList.Add("ItemName", ddlItem.SelectedItem.Text);
            sdList.Add("OldAllowance", oldAllowance);
            sdList.Add("NewAllowance", newAllowance);

            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            string info = "";
            if (empChangeBiz.Insert("Wage_Change", sdList))
            {
                info = "增加工资项变动信息成功";
                //更改员工部门
                SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
                hList.Add("EmpCode", txtEmpCode.Text.Trim());
                hList.Add(wageItem, newAllowance);
                if (empItemBiz.Update("Wage_EmpItem", hList)) 
                    info += "\n更新了基本工资项！";
                else info += "\n更新信息失败";
            }
            else
                info = "增加工资项变动信息成功";
            JSUtility.AlertAndRedirect(info, "EmpItemChangeAdd.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            if (!AccountBiz.CheckRight(account, "EmpItemChangeAdd.aspx", RightChar.Mod))
            {
                if (!AccountBiz.CheckRight(account, "EmpItemChangeList.aspx", RightChar.Mod))
                {
                    JSUtility.Alert(this, "您没有增加权限！");
                    return;
                }
            }
            string empCode = txtEmpCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(empCode))
            {
                JSUtility.Alert(this, "员工编号必须填写");
                return;
            }
            if (!employeeBiz.IsExistEmpCode(empCode))
            {
                JSUtility.Alert(this, "员工编号不存在");
                return;
            }
            string wageItem = ddlItem.SelectedValue;
            if (wageItem == "0" || ddlSetsID.SelectedValue == "0" || ddlChangeTypeName.SelectedValue == "0")
            {
                JSUtility.Alert(this, "请选择帐套、工资项、变动内容");
                return;
            }
            string changeTime = txtChangeTime.Text.Trim();
            if (!RegexRule.IsDate(changeTime))
            {
                JSUtility.Alert(this, "离职时间无效");
                return;
            }
            string oldAllowance = txtOldAllowance.Text.Trim();
            string newAllowance = txtNewAllowance.Text.Trim();

            if (!RegexRule.IsDecimal(oldAllowance) || !RegexRule.IsDecimal(newAllowance))
            {
                JSUtility.Alert(this, "数字格式必须正确");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ChangeID", changeID);
            sdList.Add("EmpCode", empCode);
            sdList.Add("EmpName", txtEmpName.Text.Trim());
            sdList.Add("StateName", txtStateName.Text.Trim());
            sdList.Add("DutyName", txtDutyName.Text.Trim());
            sdList.Add("DeptName", txtDeptName.Text.Trim());
           
            sdList.Add("ZipCode", txtZipCode.Text.Trim());
            sdList.Add("ChangeTypeName", ddlChangeTypeName.SelectedItem.Text.Trim());
            sdList.Add("ChangeTime", changeTime);
            sdList.Add("SetsID", ddlSetsID.SelectedValue);
            sdList.Add("WageItem", wageItem);
            sdList.Add("ItemName", ddlItem.SelectedItem.Text);
            sdList.Add("OldAllowance", oldAllowance);
            sdList.Add("NewAllowance", newAllowance);

            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            if (changeID != "0")
            {
                string info = "";
                if (empChangeBiz.Update("Wage_Change", sdList))
                {
                    info = "更新工资项变动信息成功";
                    //更改员工部门
                    SortedDictionary<string, string> hList = new SortedDictionary<string, string>();
                    hList.Add("EmpCode", txtEmpCode.Text.Trim());
                    hList.Add(wageItem, newAllowance);
                    if (empItemBiz.Update("Wage_EmpItem", hList))
                        info += "\n更新了基本工资项！";
                    else info += "\n更新信息失败";
                }
                else
                    info = "更新信息失败";
                JSUtility.Alert(this, info);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "EmpItemChangeAdd.aspx", RightChar.Del))
            {
                if (!AccountBiz.CheckRight(account, "EmpItemChangeList.aspx", RightChar.Del))
                {
                    JSUtility.Alert(this, "您没有删除权限！");
                    return;
                }
            }
            if (changeID != "0")
            {
                if (empChangeBiz.DeleteOneInfo(changeID))
                {
                    //不必更新为原来的职补
                    JSUtility.Alert(this, "删除成功");
                }
                else
                    JSUtility.Alert(this, "删除失败");
            }
            else
                JSUtility.Alert(this, "请先选择数据");
        }



        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }
    }
}